﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace PaiKe.Core.FileDataBase
{
    public class PictureBus : IPicture
    {
        private static readonly PictureBus pictureBus = null;
        public static string pass="sos123";
        private static bool isInitialized = false;
        private static bool isPostStarted = false;
        private static object initLocker = new object();
        private static object locker = new object();

        private PackFileManager packFileManager;
        private List<string> packFileAlls;
        private PackFile currentPackFile;
        private int currentIndex = 0;
        static PictureBus()
        {
            if (!isInitialized)
            {
                lock (initLocker)
                {
                    if (!isInitialized)
                    {
                        if (null == pictureBus)
                        {
                            pictureBus = new PictureBus();
                        }
                        pictureBus.InitPictureBus();
                    }
                }
            }
        }

        private PictureBus()
        {
            this.packFileManager = new PackFileManager();
            this.packFileAlls = new List<string>();
        }

        private void InitPictureBus()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static PictureBus GetInstance()
        {
            return pictureBus;
        }

        public PackFileManager GetPackFileManager()
        {
            if (packFileManager == null)
                packFileManager = new PackFileManager();
            return packFileManager;
        }


        public void LoadPackFile()
        {
            if (packFileManager == null)
                packFileManager = new PackFileManager();

            packFileManager.LoadPackFile("我的jpg照片集.cf");
            InitPackFile();
            InitCurrentPackFile();
        }

        public void LoadPackFile(string path)
        {

            if (packFileManager == null)
                packFileManager = new PackFileManager();

            packFileManager.LoadPackFile(path);
            InitPackFile();
            InitCurrentPackFile();
        }

        /// <summary>
        /// 加载所有。。。
        /// </summary>
        public void InitPackFile()
        {
            if (packFileManager == null)
                packFileManager = new PackFileManager();
            if (packFileAlls == null)
                packFileAlls = new List<string>();

            PackFile packFile = packFileManager.CurrentPackFile;
            foreach (string f in packFile.NameList)
            {
                packFileAlls.Add(f);
            }
        }

        public void InitCurrentPackFile()
        {
            if (packFileManager == null)
                packFileManager = new PackFileManager();
            currentPackFile = packFileManager.CurrentPackFile;
        }

        public int GetPackFilesCount()
        {
            if (packFileAlls != null)
                return packFileAlls.Count;
            return 0;
        }

        public List<string> GetPackFiles()
        {
            return packFileAlls;
        }

        /// <summary>
        /// 获得指定图片
        /// </summary>
        /// <param name="currentIndex">当前图片位置</param>
        /// <returns></returns>
        public Image GetImage(int currentIndex)
        {
            Image im = null;
            if (packFileManager == null)
                packFileManager = new PackFileManager();
            if (packFileAlls == null)
                packFileAlls = new List<string>();
            if (currentIndex >= 0)
            {
                currentIndex = currentIndex - 1;
                Stream ms = currentPackFile.GetStream(currentIndex);
                im = Image.FromStream(ms);
            }
            return im;
        }

        /// <summary>
        /// 获得左边的图片
        /// </summary>
        /// <param name="currentIndex">当前图片位置</param>
        /// <returns></returns>
        public Image GetLeftImage()
        {
            Image im = null;
            if (packFileManager == null)
                packFileManager = new PackFileManager();
            if (packFileAlls == null)
                packFileAlls = new List<string>();
            if (currentIndex > 0)
            {
                currentIndex = currentIndex - 1;
                Stream ms = currentPackFile.GetStream(currentIndex);
                im = Image.FromStream(ms);
            }
            return im;
        }

        /// <summary>
        /// 获得右边的图片
        /// </summary>
        /// <param name="currentIndex">当前图片位置</param>
        /// <returns></returns>
        public Image GetRightImage()
        {
            Image im = null;
            if (packFileManager == null)
                packFileManager = new PackFileManager();
            if (packFileAlls == null)
                packFileAlls = new List<string>();
            if (currentIndex + 1 < GetPackFilesCount())
            {
                currentIndex = currentIndex + 1;
                Stream ms = currentPackFile.GetStream(currentIndex);
                im = Image.FromStream(ms);
            }
            return im;
        }
    }
}
